import {
  tradeList
} from '../../apis/card';
const app = getApp();

Page({
  data: {
    tradeList: [],
    mainTradeIndex: 0,
    secondaryTradeIndex: 0,
    selectedTradeId: null
  },

  onLoad() {
    this.getTradeList();
  },

  getTradeList() {
    tradeList().then(res => {
      if (res.data.code === 200) {
        this.setData({
          tradeList: res.data.data
        });
      };
    });
  },

  // 切换主行业
  changeTrade(e) {
    let {
      index
    } = e.currentTarget.dataset;
    this.setData({
      mainTradeIndex: index
    });
  },

  // 选中子行业
  selectTrade(e) {
    let {
      index,
      id
    } = e.currentTarget.dataset;
    this.setData({
      secondaryTradeIndex: index,
      selectedTradeId: id
    });
  },

  confirm() {
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    prevPage.setData({
      trade: this.data.tradeList[this.data.mainTradeIndex].industryList[this.data.secondaryTradeIndex]
    });
    wx.navigateBack({
      delta: 1
    });
  }
})